﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using CML.Intercamber.Business.Constants;
using CML.Intercamber.Business.Helper;

// ReSharper disable once CheckNamespace
namespace CML.Intercamber.Business
{
    public partial class Users
    {
        public DateTime? LastMessageScored = null;
        public int NbMessageSinceLastScored = 0;
        public int NumberContacts = 0;

        public string UserNameComplete
        {
            get
            {
                return FirstName + " " + LastName;
            }
        }

        public bool IsAdmin {
            get
            {
                return this.IdProfil == ProfilsConst.Admin.Id;
            }
        }

        public bool IsTeacher
        {
            get
            {
                return this.IdProfil == ProfilsConst.Teacher.Id;
            }
        }
        /// <summary>
        /// true if main profil information is missing
        /// </summary>
        public bool MainInformationMissing
        {
            get
            {
                return string.IsNullOrEmpty(FirstName) || string.IsNullOrEmpty(LastName) || IdGender == null ||
                       BirthDate == null || string.IsNullOrEmpty(City) || string.IsNullOrEmpty(Nationality) ||
                       string.IsNullOrEmpty(ScholarLevel) || string.IsNullOrEmpty(PresentationText);
            }
        }

        #region user photo management 
        /// <summary>
        /// Save photo, and modify bean user to match these changes 
        /// does not update any data in database
        /// </summary>
        public static void AddPhoto(Users user, HttpPostedFileBase postedFile, string basePhotosDirectory)
        {
            if (postedFile == null)
                return;

            string extension = Path.GetExtension(postedFile.FileName).ToLower();
            List<string> authorizedExtensions = new List<string> { ".jpg", ".jpeg", ".png"};
            if(!authorizedExtensions.Contains(extension))
                throw new FileLoadException();

            if (!Directory.Exists(basePhotosDirectory))
                Directory.CreateDirectory(basePhotosDirectory);

            // single directory per user
            if (user.PhotoDirectory == null)
            {
                Guid? idDirectory;
                while (true)
                {
                    idDirectory = Guid.NewGuid();
                    if (!Directory.Exists(Path.Combine(basePhotosDirectory, idDirectory.ToString())))
                        break;
                }
                user.PhotoDirectory = idDirectory.Value;
            }
            if (!Directory.Exists(Path.Combine(basePhotosDirectory, user.PhotoDirectory.ToString())))
                Directory.CreateDirectory(Path.Combine(basePhotosDirectory, user.PhotoDirectory.ToString()));

            // save photo as {guid}.png
            string fileName;
            Guid? fileGuid;
            while (true)
            {
                fileGuid = Guid.NewGuid();
                fileName = Path.Combine(basePhotosDirectory, user.PhotoDirectory.ToString(), fileGuid + ".png");
                if (!File.Exists(fileName))
                    break;
            }
            user.PhotoProfile = fileGuid;

            // copy & resize photo
            var scaledImage = ImageHelper.ScaleImage(Image.FromStream(postedFile.InputStream, true, true), 550, 550);
            scaledImage.Save(fileName, ImageFormat.Png);
        }


        public static void DeletePhoto(Users user, string photoFileName)
        {
            // TODO !
        }
        #endregion
    }
}
